package vip.mall.modular.message.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.mall.common.result.CommonPageResult;
import vip.mall.modular.comment.params.CommentPageParams;
import vip.mall.modular.message.entity.Message;
import vip.mall.modular.message.mapper.MessageMapper;
import vip.mall.modular.message.params.AddMessageParams;
import vip.mall.modular.message.service.MessageService;

/**
 * @author Asus
 * @Date 2025/2/10 15:25
 * @Descripition
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    @Override
    public CommonPageResult pageList(CommentPageParams commentPageParams) {
        Page<Message> page = new Page<>(commentPageParams.getPageNum(), commentPageParams.getPageSize());
        Page<Message> messagePage = this.page(page);
        if (ObjectUtil.isNotNull(messagePage)) {
            return CommonPageResult.builder()
                    .data(messagePage.getRecords())
                    .pageNum((int) messagePage.getCurrent())
                    .pageSize((int) messagePage.getSize())
                    .total(messagePage.getTotal())
                    .totalPage((int) messagePage.getPages())
                    .build();

        }
        return null;
    }


    @Override
    public boolean add(AddMessageParams addMessageParams) {
        Message message = BeanUtil.copyProperties(addMessageParams, Message.class);
        if (ObjectUtil.isAllNotEmpty(message.getGoodsId(), message.getUserId(), message.getContent(), message.getState()))
            return this.save(message);
        return false;
    }
}
